<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<#macro mapperEl value>${r"#{"}${value}}</#macro>

<!-- 不使用namespace的话sql搜索定位会比较方便 -->
<mapper namespace="${className}">

    <resultMap id="rs_base" type="${className}">
        <#list fields as field>
        <result property="${field.field}" column="${field.column}"/>
        </#list>
    </resultMap>

     <!-- 用于select查询公用抽取的列 -->
     <sql id="columns">
        <#list columns as column>${column.column}<#if column_has_next>,</#if></#list>
     </sql>

    <!--全局where条件-->
     <sql id="where">
      <where>
        <#list fields as field>
        <#if field.checkDate>
          <if test="${field.field}Begin!=null">
             AND ${field.column} >= <@mapperEl field.field+"Begin"/>
          </if>
          <if test="${field.field}End!=null">
             AND ${field.column} &lt;= <@mapperEl field.field+"End"/>
          </if>
        <#elseif field.checkState>
          <if test="${field.field}s!=null and ${field.field}s!=''">
              AND ${field.column} in
              <foreach collection="${field.field}s" index="index" item="stateIn" open="(" separator="," close=")">
                  <@mapperEl stateIn/>
              </foreach>
          </if>
        <#else>
        <if test="${field.field}!=null and ${field.field}!=''">
            AND ${field.column} = <@mapperEl field.field/>
        </if>
        </#if>
        </#list>
      </where>
     </sql>

    <!--向${tableName}插入一条数据-->
     <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="${className}">
       INSERT INTO `${tableName}` (
       <#list columns as column>
        ${column.column} <#if column_has_next>,</#if>
        </#list>
        ) VALUES (
       <#list fields as field>
         <@mapperEl field.field/> <#if field_has_next>,</#if>
        </#list>
        )
      <selectKey keyProperty="id" resultType="long">
      select LAST_INSERT_ID()
      </selectKey>
     </insert>

    <!--根据任意条件删除${tableName}信息-->
    <delete id="delete">
      DELETE FROM `${tableName}`
      <include refid="where"/>
    </delete>

    <!--根据主键联合更新 ${tableName} 的数据-->
     <update id="update" parameterType="${className}">
       UPDATE `${tableName}`
       <trim prefix="set" suffixOverrides=",">
        <#if (notPkFields?size>0)>
         <#list notPkFields as field>
           <#if field.checkDate>
           <if test="${field.field}!=null">
               ${field.column} = <@mapperEl field.field/> <#if field_has_next>,</#if>
           </if>
           <#else>
           <if test="${field.field}!=null and ${field.field}!=''">
               ${field.column} = <@mapperEl field.field/> <#if field_has_next>,</#if>
           </if>
           </#if>
         </#list>
         </#if>
       </trim>
        <where>
        <#list fields as field>
        <#if field.checkDate>
          <if test="${field.field}Begin!=null">
             AND ${field.column} >= <@mapperEl field.field+"Begin"/>
          </if>
          <if test="${field.field}End!=null">
             AND ${field.column} &lt;= <@mapperEl field.field+"End"/>
          </if>
        <#elseif field.checkState>
          <if test="${field.field}s!=null and ${field.field}s!=''">
              AND ${field.column} in
              <foreach collection="${field.field}s" index="index" item="stateIn" open="(" separator="," close=")">
                  <@mapperEl stateIn/>
              </foreach>
          </if>
        <#elseif field.checkPk>
          <if test="${field.field}!=null and ${field.field}!=''">
             AND ${field.column} = <@mapperEl field.field/>
          </if>
        </#if>
        </#list>
        </where>

     </update>

     <!--根据任何条件加载一条${tableName}的数据-->
     <select id="load" resultMap="rs_base">
       SELECT <include refid="columns" />
       FROM `${tableName}`
       <include refid="where"/>
     </select>

     <!--根据任何条件统计${tableName}数据条数-->
      <select id="count" resultType="long">
        SELECT count(1) FROM `${tableName}`
        <include refid="where"/>
      </select>

      <!--根据任何条件分页查询${tableName}-->
      <select id="query" resultMap="rs_base">
        SELECT <include refid="columns" />
        FROM `${tableName}`
        <include refid="where"/>

        <if test="sortColumns!=null and sortColumns!=''">
        ORDER BY ${r'${sortColumns}'}
        </if>
      </select>


</mapper>

